/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbAdvs.cpp			*/
/********************************************************/

#include "GlbRtbAdvs.h"

void CGlbRtbAdvs::GlbRtbBaseCount()
{
	UINT uiIndex = 0;

	while (uiIndex < GLB_RA_SUM) {
		if ('\0' != ((GLBRAINFO_S*)m_CSet.GlbSetIndexGet(uiIndex))->m_szAdId[0]) {
			m_CSet.GlbSetPutCount(m_CSet.GlbSetGetCount() + 1);
		}
		else {
			break;
		}
		uiIndex += 1;
	}

	return;
}

int CGlbRtbAdvs::GlbRtbBaseInput(void* pPara)
{
	GLBRAINFO_S stInfo;
	xmlDoc* pstDoc = NULL;
	xmlNode* pstRoot = NULL;
	xmlNode* pstAdvs = NULL;
	int iReturn = GLB_SUCCESS;

	if (NULL != (pstDoc = GlbRaXmlFile((char*)pPara))) {
		if (NULL != (pstRoot = GlbRaXmlRoot(pstDoc))) {
			if (GLB_SUCCESS == strcmp(GLB_RA_NAME(pstRoot), (char*)"policys")) {
				GLB_RA_LOOP(pstRoot, pstAdvs)
				if (GLB_SUCCESS == strcmp(GLB_RA_NAME(pstAdvs), (char*)"policy")) {
					memset(&stInfo, '\0', sizeof(GLBRAINFO_S));
					GlbRtbAdvsParse(pstAdvs, &stInfo);
					m_CSet.GlbSetIndexPut(m_CSet.GlbSetGetCount(), &stInfo);
				}
				GLB_RA_LOOPEND
				m_CSet.GlbSetSort();
			}
		}
		else {
			GLB_ERROR("Failed to GlbRaXmlRoot\n");
			iReturn = GLB_FAILURE;
		}
		GlbRaXmlFree(pstDoc);
		GlbRaXmlClean();
	}
	else {
		GLB_ERROR("Failed to GlbRaXmlFile\n");
		iReturn = GLB_FAILURE;
	}

	return iReturn;
}

void CGlbRtbAdvs::GlbRtbAdvsDisplay()
{
	UINT uiCount = 0;

	while (uiCount < m_CSet.GlbSetGetCount()) {
		GLB_PRINT("Count[%04d]:\n", uiCount + 1);
		GlbRtbAdvsPrint((GLBRAINFO_S*)m_CSet.GlbSetIndexGet(uiCount ++));
		GLB_PRINT("\n");
	}

	return;
}

void GlbRtbAdvsPrint(GLBRAINFO_S* pstInfo)
{
	GLB_PRINT("{policyID: %s} + ", pstInfo->m_szAdId);
	GLB_PRINT("{start: %s} + ", pstInfo->m_szBgTm);
	GLB_PRINT("{end: %s} + ", pstInfo->m_szEdTm);
	GLB_PRINT("{browser: %s} + ", pstInfo->m_szBrow);
	GLB_PRINT("{os: %s} + ", pstInfo->m_szOsNm);
	GLB_PRINT("{location: %s} + ", pstInfo->m_szLoca);
	GLB_PRINT("{mediatype: %s} + ", pstInfo->m_szMdTy);
	GLB_PRINT("{media: %s} + ", pstInfo->m_szMedi);
	GLB_PRINT("{priority: %s} + ", pstInfo->m_szPrio);
	GLB_PRINT("{volume: %s} + ", pstInfo->m_szVolu);
	GLB_PRINT("{price: %s} + ", pstInfo->m_szPric);
	GLB_PRINT("{url: %s} + ", pstInfo->m_szCUrl);
	GLB_PRINT("{screen: %s} + ", pstInfo->m_szSrcn);
	GLB_PRINT("{type: %s}\n", pstInfo->m_szType);

	return;
}

void GlbRtbAdvsParse(xmlNode* pstNode, GLBRAINFO_S* pstInfo)
{
	UINT uiCount = 0;
	xmlNode* pstTemp = NULL;

	GLBRANODE_S stNode[] = {
		{(char*)"policyID", pstInfo->m_szAdId},
		{(char*)"start", pstInfo->m_szBgTm},
		{(char*)"end", pstInfo->m_szEdTm},
		{(char*)"browser", pstInfo->m_szBrow},
		{(char*)"os", pstInfo->m_szOsNm},
		{(char*)"location", pstInfo->m_szLoca},
		{(char*)"mediatype", pstInfo->m_szMdTy},
		{(char*)"media", pstInfo->m_szMedi},
		{(char*)"priority", pstInfo->m_szPrio},
		{(char*)"volume", pstInfo->m_szVolu},
		{(char*)"price", pstInfo->m_szPric},
		{(char*)"url", pstInfo->m_szCUrl},
		{(char*)"screen", pstInfo->m_szSrcn},
		{(char*)"type", pstInfo->m_szType},
	};

	GLB_RA_LOOP(pstNode, pstTemp)
	if (GLB_RA_TYPE(pstTemp) && GLB_SUCCESS == strcmp(GLB_RA_NAME(pstTemp), stNode[uiCount].m_pszKey)) {
		GlbRaXmlVal(pstTemp, stNode[uiCount ++].m_pszVal);
	}
	GLB_RA_LOOPEND

	return;
}
